package com.gopay.common.user.dao.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.Constants;
import com.gopay.common.constants.cust.RelSourceConstans;
import com.gopay.common.constants.trans.GatewaySource;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.cloud.CloudCustAcctRel;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.common.user.dao.CloudCustAcctRelDao;

/**
 * 
 * ZhangZG
 * 2015年12月29日 
 * www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("cloudCustAcctRelDao")
public class CloudCustAcctRelDaoImpl extends BaseDAOImpl<CloudCustAcctRel, Serializable> implements CloudCustAcctRelDao {

	 @Resource(name = "commonQueryDAO")
	 private CommonQueryDAO commonQueryDAO;
	
	/**
	 * 根据平台客户号和子公司帐号获取关联实体
	 * @param partnerCustId  平台custId
	 * @param acctId  子公司账户
	 */
	@Override
    public CloudCustAcctRel findCloudCustAcctRelByAcct(String partnerCustId, String acctId) {

		String hql = "from CloudCustAcctRel t where t.acctId=:acctId and t.partnerCustId=:partnerCustId";
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("partnerCustId", partnerCustId);
		map.put("acctId", acctId);

		List<CloudCustAcctRel> result = this.findByHql(hql, map);
		return result != null && result.size() > 0 ? result.get(0) : null;
	}

	/**
	 * 通过协议号获取关联信息
	 * @param agreeId
	 * @return
	 */
	@Override
    public CloudCustAcctRel findCloudCustAcctRelByAgreeId(String agreeId) {
		String hql = "from CloudCustAcctRel t where t.agreeId=:agreeId";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("agreeId", agreeId);

		List<CloudCustAcctRel> result = this.findByHql(hql, map);
		return result != null && result.size() > 0 ? result.get(0) : null;
	}

	/**
	 * 根据子公司客户号和子公司的唯一Id现为邮箱获取关联实体
	 * @param custId  子公司custId
	 * @param partnerUserId  子公司账户
	 */
	@Override
    public CloudCustAcctRel findCloudCustAcctRelByUserId(String custId, String partnerUserId) {

		String hql = "from CloudCustAcctRel t where t.custId=:custId and t.partnerUserId=:partnerUserId";
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("custId", custId);
		map.put("partnerUserId", partnerUserId);

		List<CloudCustAcctRel> result = this.findByHql(hql, map);
		return result != null && result.size() > 0 ? result.get(0) : null;
	}

	/**
	 * 根据采购部客户号和子公司的唯一Id现为邮箱获取关联实体
	 * @param partnerCustId  平台custId
	 * @param partnerUserId  邮箱
	 */
	@Override
    public CloudCustAcctRel findCloudPaCustAcctRelByUserId(String partnerCustId, String partnerUserId) {

		String hql = "from CloudCustAcctRel t where t.partnerCustId=:partnerCustId and t.partnerUserId=:partnerUserId";
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("partnerCustId", partnerCustId);
		map.put("partnerUserId", partnerUserId);

		List<CloudCustAcctRel> result = this.findByHql(hql, map);
		return result != null && result.size() > 0 ? result.get(0) : null;
	}

	/**
	 * 以下方法已LiveWalletCustAcctRelDaoImpl为准，这里放一份，主要是为了实现类不报没实现接口方法的错误 20160323 tangyang
	 */

	/**
	 * 根据合作方客户号、合作方账户号和用户协议号确定唯一关联实体
	 * @param partnerCustId  平台custId
	 * @param partnerAcctId  平台账户
	 * @param agreeId  协议号
	 */
	@Override
    public CloudCustAcctRel findCustAcctRelByAcctIdCustIdAndAgreeId(String partnerCustId, String partnerAcctId, String agreeId) {

		String hql = "from CloudCustAcctRel t where t.partnerCustId=:partnerCustId and t.partnerAcctId=:partnerAcctId and t.agreeId=:agreeId and t.stat = 1";
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("partnerCustId", partnerCustId);
		map.put("partnerAcctId", partnerAcctId);
		map.put("agreeId", agreeId);

		List<CloudCustAcctRel> result = this.findByHql(hql, map);
		return result != null && result.size() > 0 ? result.get(0) : null;
	}

	/**
	 * 根据平台custId、子custId、子账户号确定唯一关联实体
	 * @param partnerCustId  平台custId
	 * @param partnerAcctId  平台账户
	 * @param custId  子custId
	 * @param acctId  子账户号
	 */
	@Override
    public CloudCustAcctRel findCustAcctRelByAcctIdCustIdNew(String partnerCustId, String partnerAcctId, String custId, String acctId) {

		String hql = "from CloudCustAcctRel t where t.stat=1 and t.partnerCustId=:partnerCustId and t.partnerAcctId=:partnerAcctId and t.custId=:custId and t.acctId=:acctId";
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("partnerCustId", partnerCustId);
		map.put("partnerAcctId", partnerAcctId);
		map.put("custId", custId);
		map.put("acctId", acctId);

		List<CloudCustAcctRel> result = this.findByHql(hql, map);
		return result != null && result.size() > 0 ? result.get(0) : null;
	}

	/**
	 * 根据合作方账户号和用户Id确定唯一关联实体
	 * @param partnerAcctId  平台账户
	 * @param mobileOrEmail  用户手机或邮箱
	 */
	@Override
    public CloudCustAcctRel findCustAcctRelByAcctIdAndUserMob(String partnerAcctId, String mobileOrEmail) {
		String hql = "from CloudCustAcctRel t where t.stat=1 and t.partnerAcctId=:partnerAcctId and t.partnerUserId=:partnerUserId";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("partnerAcctId", partnerAcctId);
		map.put("partnerUserId", mobileOrEmail);
		List<CloudCustAcctRel> result = this.findByHql(hql, map);
		return result != null && result.size() > 0 ? result.get(0) : null;
	}

	/**
	 * 根据合作方客户号、合作方商户的客户号 并且是来源于生钱包的签约
	 * @param partnerCustId
	 * @param custId
	 * @return
	 */
	@Override
	public CloudCustAcctRel findCustAcctRelByPartnerCustIdAndCustId(String partnerCustId, String custId, String transferOutAcctId, String transferInAcctId) {
		String hql = "from CloudCustAcctRel t where t.stat=1 and t.source=:source and t.partnerCustId=:partnerCustId " + "and t.custId=:custId and partner_acct_id=:transferOutAcctId and acct_id=:transferInAcctId";
		Map<String, Object> map = new HashMap<>();
		map.put("partnerCustId", partnerCustId);
		map.put("custId", custId);
		map.put("source", RelSourceConstans.LIVE_WALLET);
		map.put("transferOutAcctId", transferOutAcctId);
		map.put("transferInAcctId", transferInAcctId);
		List<CloudCustAcctRel> result = this.findByHql(hql, map);
		return result != null && result.size() > 0 ? result.get(0) : null;
	}
	/**
	 * 根据合作方客户号，合作方商户的客户号确定唯一关联实体
	 * @param custId
	 * @param partnerCustId
	 * @return
	 */
	@Override
    public CloudCustAcctRel findCustAcctRelByCustIdAndPartnerCustId(String custId, String partnerCustId) {
		String hql = "from CloudCustAcctRel t where t.stat=1 and t.partnerCustId=:partnerCustId and t.custId=:custId";
		Map<String, Object> map = new HashMap<>();
		map.put("partnerCustId", partnerCustId);
		map.put("custId", custId);
		List<CloudCustAcctRel> result = this.findByHql(hql, map);
		return result != null && result.size() > 0 ? result.get(0) : null;
	}
	
	
	@Override
    public String getCloudAcct(CpsGenMainOrder mainOrder) {
		
		
		if (StringUtils.equals(GatewaySource.GATEWAY_SOURCE_06.value,
				getGatewaySource(mainOrder.getGopayOrderId()))) {
			String cloudCustId = mainOrder.getMerId();
			String acctId = mainOrder.getPayAcct();
			CloudCustAcctRel cloudRel = findCloudCustAcctRelByAcct(cloudCustId,
					acctId);
			if (cloudRel != null) {
				return cloudRel.getPartnerAcctId();
			} else if (Constants.GOPAY_SYS_MERID.equals(cloudCustId)) {
				return acctId;
			}
			//如果是云账户网关支付
			if(StringUtils.equals(IntTxnCd._00700.value, mainOrder.getGopayIntTxnCd())){
				return acctId;
			}
			
		} else {
			return mainOrder.getPayAcct();
		}
		return null;
	}
	
	@Override
    public String getCloudCustId(CpsGenMainOrder mainOrder) {
		
		
		if (StringUtils.equals(GatewaySource.GATEWAY_SOURCE_06.value,
				getGatewaySource(mainOrder.getGopayOrderId()))) {
			String cloudCustId = mainOrder.getMerId();
			String acctId = mainOrder.getPayAcct();
			CloudCustAcctRel cloudRel = findCloudCustAcctRelByAcct(cloudCustId,
					acctId);
			if (cloudRel != null) {
				return cloudRel.getPartnerCustId();
			} else if (Constants.GOPAY_SYS_MERID.equals(cloudCustId)) {
				return mainOrder.getPayCustId();
			}
			
			//如果是云账户网关支付
			if(StringUtils.equals(IntTxnCd._00700.value, mainOrder.getGopayIntTxnCd())){
				return cloudCustId;
			}
			
		} else {
			return mainOrder.getPayCustId();
		}
		return null;
	}
	
	@Override
	public String getCloudAcct(String acctId, String merId) {
	    CloudCustAcctRel cloudRel = findCloudCustAcctRelByAcct(merId,acctId);
        if (cloudRel != null) {
            return cloudRel.getPartnerAcctId();
        }
        return null;
	}

	@Override
	public CloudCustAcctRel findCustAcctRealByAgreeIdAndPartnerCustId(String agreeId, String partnerCustId) {
		String hql = "from CloudCustAcctRel t where t.agreeId=:agreeId and t.partnerCustId=:partnerCustId";
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("agreeId", agreeId);
		map.put("partnerCustId", partnerCustId);

		List<CloudCustAcctRel> result = this.findByHql(hql, map);
		return result != null && result.size() > 0 ? result.get(0) : null;
	}

	@Override
	public CloudCustAcctRel findCustAcctRealByPartnerUserIdAndPartnerCustId(String partnerUserId, String partnerCustId) {
		String hql = "from CloudCustAcctRel t where t.partnerUserId=:partnerUserId and t.partnerCustId=:partnerCustId";
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("partnerUserId", partnerUserId);
		map.put("partnerCustId", partnerCustId);

		List<CloudCustAcctRel> result = this.findByHql(hql, map);
		return result != null && result.size() > 0 ? result.get(0) : null;
	}

	@Override
	public List<CloudCustAcctRel> findCloudCustAcctRelList(String partnerCustId) {
		String hql = "from CloudCustAcctRel t where t.stat=1 and t.partnerCustId=:partnerCustId";
		Map<String, Object> map = new HashMap<>();
		map.put("partnerCustId", partnerCustId);
		List<CloudCustAcctRel> result = this.findByHql(hql, map);
		return result;
	}

	private String getGatewaySource(String orderId){
		String sql = "select t.gateway_source,t.gopay_order_id from  cps_gen_main_order t where t.gopay_order_id=:orderId";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orderId", orderId);
		List<Object[]> rsList = commonQueryDAO.findBySql(sql, map);

		if (rsList != null && rsList.size() > 0) {
			return  (String)rsList.get(0)[0];
		}
		return null;
	}

	@Override
	public CloudCustAcctRel findByCustId(String custId) {
		String hql = "from CloudCustAcctRel t where t.stat=1 and t.custId=:custId";
		Map<String,Object> queryParam = new HashMap<>(1);
		queryParam.put("custId",custId);
		List<CloudCustAcctRel> list = findByHql(hql,queryParam);
		if(list != null && !list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<CloudCustAcctRel> findSubCloudAccts(String partnerCustId, String partnerAcctId) {
		String hql = "from CloudCustAcctRel t where t.stat=1 and t.partnerCustId=:partnerCustId and t.partnerAcctId =:partnerAcctId ";
		Map<String, Object> map = new HashMap<>();
		map.put("partnerCustId", partnerCustId);
		map.put("partnerAcctId", partnerAcctId);
		List<CloudCustAcctRel> result = this.findByHql(hql, map);
		return result;
	}
}
